<?php
require_once dirname(__FILE__).'/../../../KenDon.php';

require_once 'PHPUnit/Framework.php';


class Test_KD_PDO_Adapter extends PHPUnit_Framework_TestCase{

    public function setUp(){
      $this->assertEquals( true,  KenDon::load('DB_PDO', 'adapter') );
    }

    /**
      *@test
      *
      */
    public function testLoadPDOAdapter(){

      $this->assertEquals( true, class_exists('KenDon', false) );

      $this->assertEquals( true,  KenDon::load('DB_PDO', 'adapter') );

      $this->assertEquals( true,  class_exists('KD_Adapter_DB_PDO' , false) );
    }


    /**
      * @expectedException
      *
      */
    /*
    public function testConnectSQLite(){


      if(){}else{}
      try{
        $dbh = new KD_Adapter_DB_PDO(
          array(
            'dbms'=>'sqlite',
            'dbName'=>  dirname(__FILE__).KDDS.'kendon.s3db' ,
          )
        );
      }catch(PDOException $e){
        echo $e->getMessage();
      }

      //$dbh = new PDO( 'sqlite2:'. dirname(__FILE__).KDDS.'kendon.sdb' );
      //$dbh = new PDO( 'sqlite2:E:\workspace\myworkspace\openprojects\KenDon\testCase\kendon.sdb');

      $this->assertEquals(true, is_a($dbh, 'KD_Adapter_DB_PDO'));
      $this->assertEquals(true, $dbh instanceof KD_Adapter_DB_PDO);
      $this->assertEquals(true, is_subclass_of($dbh, 'PDO'));

      $record = $dbh->query('SELECT * FROM [qtree]');

      $this->assertEquals(true, is_a($record,  'PDOStatement') );

      //$rs = $record->fetchAll();

      //$this->assertEquals(true, is_array($rs) );

      //$this->assertEquals(true, $rs);

      //$this->markTestIncomplete('未完成测试代码'); //已完成

    }
    */

    /**
     * @expectedException
     */
    public function testConnectSQLite2(){

      try{
        $dbh = new KD_Adapter_DB_PDO(
          array(
            'dbms'=>'sqlite2',
            'dbName'=>  dirname(__FILE__).'\..\..\kendon.sdb' ,
          )
        );
      }catch(PDOException $e){
        echo $e->getMessage();
      }

      //$dbh = new PDO( 'sqlite2:'. dirname(__FILE__).KDDS.'kendon.sdb' );
      //$dbh = new PDO( 'sqlite2:E:\workspace\myworkspace\openprojects\KenDon\testCase\kendon.sdb');

      $this->assertEquals(true, is_a($dbh, 'KD_Adapter_DB_PDO'));
      $this->assertEquals(true, $dbh instanceof KD_Adapter_DB_PDO);
      $this->assertEquals(true, is_subclass_of($dbh, 'PDO'));

      $record = $dbh->query('SELECT * FROM [qtree]');

      $this->assertEquals(true, is_a($record,  'PDOStatement') );

      $rs = $record->fetchAll();

      $this->assertEquals(true, is_array($rs) );

      //$this->assertEquals(true, $rs);

      //$this->markTestIncomplete('未完成测试代码'); //已完成
    }


    /**
     * @expectedException
     */
    public function testConnectMySQL(){
      //$this->markTestIncomplete('未完成测试代码');

      try{
        $dbh = new KD_Adapter_DB_PDO(
          array(
            'dbms'=>'mysql' ,
            'dbName'=>  'kendon' ,
          )
        );
      }catch(PDOException $e){
        echo $e->getMessage();
      }

      $this->assertEquals(true, is_a($dbh, 'KD_Adapter_DB_PDO'));
      $this->assertEquals(true, $dbh instanceof KD_Adapter_DB_PDO);
      $this->assertEquals(true, is_subclass_of($dbh, 'PDO'));

      $record = $dbh->query('SELECT * FROM `qtree`;');

      $this->assertEquals(true, is_a($record,  'PDOStatement') );

      $rs = $record->fetchAll();

      $this->assertEquals(true, is_array($rs) );
    }
}
